#### Blatte, Piccoli, Zachem Replication - Appendix Section 2.2.1 #### 

#### PACKAGES ####
library(dplyr)
library(stargazer)

sink("../output/replicate_bpz.log")

# data 
poll <- read.csv("../input/poll_2022_weighted_final_topost.csv")

# define inconsistent

poll$Inconsistent <- ifelse(poll$Grid.Question_3 == 1 & poll$Grid.Question_4 ==1, "Inconsistent", "Consistent")
poll$Inconsistent[poll$increasefunds==1 &poll$decreasefunds==1] <- "Inconsistent"
poll$Inconsistent[is.na(poll$Inconsistent)] <- "Consistent"

# define endorsement condition

poll$endorsed <- "Control"

poll$endorsed[poll$endorsement=='Former President Trump has given Terry Mitchell his “complete and total endorsement!"'] <- "trumpYes"

poll$endorsed[poll$endorsement=='Former President Trump has said, “I will NOT be endorsing Terry Mitchell for Congress!”'] <- "trumpNo"

poll$endorsed <- factor(poll$endorsed, levels=c("trumpYes","trumpNo","Control"))

poll$endorsed <- relevel(poll$endorsed,ref="Control")

# define stance condition

poll$stance[poll$conventional=='Terry supports lowering taxes on wealthy individuals and large corporations, wants to limit the role of the federal government in the health insurance marketplace, and opposes a path to citizenship for undocumented immigrants.'] <- "convenR"

poll$stance[poll$conventional=='Terry supports raising taxes on wealthy individuals and large corporations, wants to expand the role of the federal government in the health insurance marketplace, and believes in a path to citizenship for undocumented immigrants.'] <- "unconvenR"

poll$stance <- factor(poll$stance, levels=c("unconvenR","convenR"))

# define party ID

poll$partyID <- NA
poll$partyID[poll$pid3==1|poll$ind_lean==1] <- "Democrat"
poll$partyID[poll$pid3==2|poll$ind_lean==2] <- "Republican"
poll$partyID[poll$ind_lean==3|poll$ind_lean==4] <- "Independent/Other"
poll$partyID <- factor(poll$partyID, levels=c("Democrat","Republican","Independent/Other"))
poll$partyID <- relevel(poll$partyID,ref="Independent/Other")


# interact stance and endorsement

poll$condition<- interaction(poll$stance, poll$endorsed, sep = "_")
poll$condition <- relevel(poll$condition,ref="unconvenR_Control")


# Recode outcome

poll$mitchell_vote_scaled <- car::recode(poll$mitchell_vote, "1=0; 2=0.25; 3=0.5; 4=0.75; 5=1")

poll_nocontradict <- subset(poll, Inconsistent == "Consistent")

#### Replicate Results ####

poll$partyID <- relevel(poll$partyID, "Independent/Other", "Republican", "Democrat")
poll_nocontradict$partyID <- relevel(poll_nocontradict$partyID, "Independent/Other", "Republican", "Democrat")


# Table 1 Column 1 with and without contradictors
stance_and_endorsement_table <- lm(mitchell_vote_scaled ~ stance + endorsed, data = poll)
stance_and_endorsement_table_n <- lm(mitchell_vote_scaled ~ stance + endorsed, data = poll_nocontradict)

# Output table #
comparison_list <- list(stance_and_endorsement_table, stance_and_endorsement_table_n)
stargazer(comparison_list, align = T, dep.var.labels = c("Likelihood of Voting for Mitchell"), covariate.labels = c("Conventional Policy", "Endorsement", "Anti-Endorsement"), digits = 2,column.labels = c("All Participants", "Contradictors Removed"), star.cutoffs = c(0.1, 0.05, 0.01), out = "../output/trump_replication.tex")
print("Table B3 Produced")
